package com.demo.dao.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import java.util.Map;

import com.demo.model.Student;


public class StudentMapper {

	public String insert(Student student) {
		return "insert into student (username, password, nickname) values (#{username},#{password},#{nickname})";
	}
	
	public String selectByUsernameAndPassward(Map<String, Object> parameter) {
		return "select * from student where username=#{username} and password=#{password}";
	}
	
	public String select(String username) {
		if (username == null || username.length() == 0) {
			return "select * from student order by id desc";
		} else {
			return "select * from student where username=#{username} order by id desc";
		}
	}

	public String selectOne(Integer id) {
		return "select * from student where id=#{id}";
	}

	public String update(Student student) {
		BEGIN();
		UPDATE("student");
		if (student.getNickname() != null && student.getNickname().length() > 0) {
			SET("nickname=#{nickname}");
		}
		if (student.getPassword() != null && student.getPassword().length() > 0) {
			SET("password=#{password}");
		}
		WHERE("id=#{id}");
		String sql = SQL();
		System.out.println(sql);
		return sql;
	}

	public String delete(long id) {
		BEGIN();
		DELETE_FROM("student");
		WHERE("id=#{id}");
		return SQL();
	}
}
